package com.qf.springcloud.order.controller;

import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixProperty;
import com.qf.springcloud.order.feign.ItemFeignClient;
import com.qf.springcloud.order.mq.SendMessagePojo;
import com.qf.springcloud.order.pojo.Order;
import com.qf.springcloud.order.pojo.OrderItem;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.messaging.MessageChannel;
import org.springframework.messaging.support.GenericMessage;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/orders")
public class OrderController {

    private SendMessagePojo sendMessagePojo;

    @Autowired
    public void setSendMessagePojo(SendMessagePojo sendMessagePojo) {
        this.sendMessagePojo = sendMessagePojo;
    }

    private ItemFeignClient itemFeignClient;

    @Autowired
    public void setItemFeignClient(ItemFeignClient itemFeignClient) {
        this.itemFeignClient = itemFeignClient;
    }

    @GetMapping("/order/{id}")
    @HystrixCommand(fallbackMethod = "kafeihaishihenbucuode", commandProperties = {@HystrixProperty(name = "execution.isolation.strategy", value = "SEMAPHORE")})
    public Order getOrderById(@PathVariable Integer id){
        System.err.println("order收到查询请求了" + Thread.currentThread().getName());
        Order order = new Order();
        order.setId(id);
        order.setAddress("沙阳路 38 号北科");
        OrderItem orderItem = itemFeignClient.getItemById(id + 1);
        order.setOrderItem(orderItem);
        int i=1/0;
        return order;
    }

    @GetMapping("/test1")
    @HystrixCommand(fallbackMethod = "test1jiangji")
    public String test1() {
        System.err.println("test1111" + Thread.currentThread().getName());
        return itemFeignClient.test1("zhangsandsg", 30);
    }

    @GetMapping("/test2")
    @HystrixCommand(fallbackMethod = "test2jiangji")//添加了注解之后如果方法的总执行时间超过 1 秒中就会失败
    public OrderItem test2() {
        System.err.println("test22222222" + Thread.currentThread().getName());
        try {
            Thread.sleep(5000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        OrderItem orderItem = new OrderItem();
        orderItem.setId(100);
        orderItem.setName("李四");
        orderItem.setPrice(-100);
        orderItem.setCategory("的闲工夫");
        return itemFeignClient.test2(orderItem);
    }

    @GetMapping("/test3")
    public String test3() {
        System.err.println("test333333" + Thread.currentThread().getName());
        return "33333333v22222222222";
    }

    @GetMapping("/test4")
    public String test4(String msg) {
        MessageChannel channel = sendMessagePojo.getChannel();//获取到通道-
        channel.send(new GenericMessage<String>(msg));//发送消息
        return "44444444";
    }

    public String test1jiangji() {
        System.err.println("test1111降级" + Thread.currentThread().getName());

        return "test1jiangji";
    }


    public OrderItem test2jiangji() {
        System.err.println("test222222降级" + Thread.currentThread().getName());
        OrderItem orderItem = new OrderItem();
        orderItem.setId(-999);
        orderItem.setName("李四");
        orderItem.setPrice(-100);
        orderItem.setCategory("的闲工夫");
        return orderItem;
    }

    public Order kafeihaishihenbucuode(Integer id, Throwable throwable) {
        System.err.println("order出现异常执行了降级的方法" + Thread.currentThread().getName());
        Order order = new Order();
        order.setId(-id);
        order.setAddress("冤鬼路 444 号 444 楼 444 室");
        order.setOrderItem(null);
        return order;
    }
}
